package edu.ccut.saturn.studio.core.util.wizard;

import java.io.File;
import java.util.Map;

import org.eclipse.core.resources.IResource;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;

import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnEclipseSelectionUtil;

public abstract class SaturnWizardPage extends WizardPage {

	private IStructuredSelection selection;

	public SaturnWizardPage(String pageName, IStructuredSelection selection) {
		super(pageName);

		this.selection = selection;
	}

	public SaturnWizardPage(String pageName) {
		super(pageName);
	}

	protected SaturnWizardPage() {
		super("");
	}

	public IStructuredSelection getSelection() {
		return selection;
	}

	public void setSelection(IStructuredSelection selection) {
		this.selection = selection;
	}

	public abstract void initalValues(Map<String, Object> values);

	protected String getFullPath() {
		return SaturnEclipseSelectionUtil.getFullPath(selection);
	}

	public boolean validate() {
		return true;
	}

	/**
	 * 返回完成的构件key，支持构件(component, logic, action, validate)
	 * 
	 * @param simpleKey
	 * @return
	 */
	protected String getComponentKey(String simpleKey) {
		String packageName = this.getPackageName();

		String spacename = ""; //$NON-NLS-1$
		try {
			spacename = "/" + packageName + "/" + simpleKey;

		} catch (Exception e1) {
			e1.printStackTrace();
		} 

		return spacename;
	}

	protected String getPackageName() {
		IResource resource = SaturnProjectUtil.getResource(selection);

		String packageName = resource.getProjectRelativePath().toOSString();
		packageName = packageName
				.substring(packageName.indexOf(File.separator) + 1);
		packageName = packageName.replace(File.separator, "/");

		return packageName;
	}
}
